#include "StdAfx.h"
#include "NDLDebugRenderer.h"



namespace _MODULE_
{
	namespace _RENDER_
	{

		
	

		NDLDebugRenderer::NDLDebugRenderer(NiRendererPtr ptrNDLRenderer)
		{
			m_NDLRenderer=ptrNDLRenderer;
			m_NiTriStripsVector.reserve(3);
			m_NiLinesVector.reserve(3);
			m_NiParticlesVector.reserve(3);

		//	SetWireFrame(true);
		}

		NDLDebugRenderer::~NDLDebugRenderer(void)
		{

			
			for(int i=0;i<m_NiTriStripsVector.size();i++)
			{
				if(m_NiTriStripsVector[i]!=NULL)
				{
					m_NiTriStripsVector[i]=NULL;
				}
			}

			for(int i=0;i<m_NiLinesVector.size();i++)
			{
				if(m_NiLinesVector[i]!=NULL)
				{
					m_NiLinesVector[i]=NULL;
				}
			}

			for(int i=0;i<m_NiParticlesVector.size();i++)
			{
				if(m_NiParticlesVector[i]!=NULL)
				{
					m_NiParticlesVector[i]=NULL;
				}
			}
			
			m_NDLRenderer=NULL;

		}

		void NDLDebugRenderer::AddPoints(_FOGGE_::DEBUGVERTEX* point,_FOGGE_::foUInt pointCount)
		{
			NiPoint3 *newPoints=foNEW NiPoint3[pointCount];
			NiColorA* newColors=foNEW NiColorA[pointCount];

			for(int i=0;i<pointCount;i++)
			{
				newPoints[i].x=point[i].v[0];
				newPoints[i].y=point[i].v[1];
				newPoints[i].z=point[i].v[2];
			}
		/*	//default normal
			for(int i=0;i<pointCount;i++)
			{
				newNormals[i]=NiPoint3::UNIT_Y;
			}
*/
			for(int i=0;i<pointCount;i++)
			{
				newColors[i].r=GetRValue(point[i].color)/256.0f;
				newColors[i].g=GetGValue(point[i].color)/256.0f;
				newColors[i].b=GetBValue(point[i].color)/256.0f;
				newColors[i].a=1;

			}
			
		//	NiParticles* ptrNewNiParticles=foNEW NiParticles(pointCount,newPoints,NULL,newColors);

		//	m_NiParticlesVector.push_back(ptrNewNiParticles);

			
		}
		void NDLDebugRenderer::AddLines(_FOGGE_::DEBUGVERTEX* point,_FOGGE_::foUInt linetCount)
		{
			_FOGGE_::foUInt pointCount=linetCount*2;
			NiPoint3 *newPoints=foNEW NiPoint3[pointCount];
			NiColorA* newColors=foNEW NiColorA[pointCount];

			for(int i=0;i<pointCount;i++)
			{
				newPoints[i].x=point[i].v[0];
				newPoints[i].y=point[i].v[1];
				newPoints[i].z=point[i].v[2];
			}
			

			for(int i=0;i<pointCount;i++)
			{
				newColors[i].r=GetRValue(point[i].color)/256.0f;
				newColors[i].g=GetGValue(point[i].color)/256.0f;
				newColors[i].b=GetBValue(point[i].color)/256.0f;
				newColors[i].a=1;

			}
			
			NiLines* ptrNewNiLines=foNEW NiLines(pointCount,newPoints,newColors,NULL,0,NiGeometryData::NBT_METHOD_NONE,NULL);

			m_NiLinesVector.push_back(ptrNewNiLines);

			
		}
		void NDLDebugRenderer::AddTriangles(_FOGGE_::DEBUGVERTEX* point,_FOGGE_::foUInt triangleCount)
		{
			_FOGGE_::foUInt pointCount=triangleCount*3;
			NiPoint3 *newPoints=foNEW NiPoint3[pointCount];
			NiColorA* newColors=foNEW NiColorA[pointCount];

			unsigned short *stripLength=foNEW unsigned short[triangleCount];
			unsigned short *stripLists=foNEW unsigned short[triangleCount*3];

			for(int i=0;i<pointCount;i++)
			{
				newPoints[i].x=point[i].v[0];
				newPoints[i].y=point[i].v[1];
				newPoints[i].z=point[i].v[2];
				
			}
			

			for(int i=0;i<pointCount;i++)
			{
				newColors[i].r=GetRValue(point[i].color)/256.0f;
				newColors[i].g=GetGValue(point[i].color)/256.0f;
				newColors[i].b=GetBValue(point[i].color)/256.0f;
				newColors[i].a=1;

			}

			for(int i=0;i<triangleCount;i++)
			{
				stripLength[i]=1;

			}
			for(int i=0;i<pointCount;)
			{
				stripLists[i]=0;
				stripLists[i++]=1;
				stripLists[i++]=2;
			}
			
			NiTriStrips* ptrNewNiTriStrips=foNEW NiTriStrips(pointCount,newPoints,NULL,newColors,NULL,0,NiGeometryData::NBT_METHOD_NONE,triangleCount,triangleCount,stripLength,stripLists);

			m_NiTriStripsVector.push_back(ptrNewNiTriStrips);

			
			
		}

		HRESULT NDLDebugRenderer::RenderDebugInfo(void)
		{

			//render tries
			for(int i=0;i<m_NiTriStripsVector.size();i++)
			{
				m_NDLRenderer->RenderTristrips(m_NiTriStripsVector[i]);
			}
			m_NiTriStripsVector.clear();

			//render lines
			for(int i=0;i<m_NiLinesVector.size();i++)
			{
				m_NDLRenderer->RenderLines(m_NiLinesVector[i]);
			}
			m_NiLinesVector.clear();

			//render points
			for(int i=0;i<m_NiParticlesVector.size();i++)
			{
				m_NDLRenderer->RenderPoints(m_NiParticlesVector[i]);
			}
			m_NiParticlesVector.clear();
			
			return S_OK;
		}


		

		
	}
}